import java.util.Arrays;
/**
 * Created with IntelliJ IDEA.
 * Description:
 * User:38917
 * Date:2025/4/13
 * Time:16:12
 */
public class Solution {
    public void setZeroes(int[][] matrix) {
        int rowLength = matrix.length, colLength = matrix[0].length;
        boolean row = false,col = false;
        for(int num : matrix[0]){//判断第一行是否存在0
            if(num == 0){
                row = true;
                break;
            }
        }
        for (int[] ints : matrix) {//判断第一列是否存在0
            if (ints[0] == 0) {
                col = true;
                break;
            }
        }
        for (int i = 1; i < rowLength; i++) {
            for (int j = 1; j < colLength; j++) {
                if (matrix[i][j] == 0) {
                    matrix[i][0] = 0;
                    matrix[0][j] = 0;
                }
            }
        }
        for (int i = 1; i < rowLength; i++) {
            for (int j = 1; j < colLength; j++) {
                if (matrix[i][0] == 0 || matrix[0][j] == 0) {
                    matrix[i][j] = 0;
                }
            }
        }
        if (row){
            for (int i = 0; i < colLength; i++) {
                matrix[0][i] = 0;
            }
        }
        if (col){
            for (int i = 0; i < rowLength; i++) {
                matrix[i][0] = 0;
            }
        }
    }
    public static void main(String[] args) {
        int[][] matrix = {{0,1,2,0},{3,4,5,2},{1,3,1,5}};
        Solution solution = new Solution();
        solution.setZeroes(matrix);
        for (int[] ints : matrix) {
            System.out.println(Arrays.toString(ints));
        }
    }
}
